package com.xlserver.controller;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.aliyuncs.utils.IOUtils;
import com.google.protobuf.ByteString;
import com.xlserver.domain.Counselor;
import com.xlserver.domain.Supervisor;
import com.xlserver.service.ScheduleService;
import com.xlserver.util.ImageParser;
import com.xlserver.util.SMSUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/schedule")
@AllArgsConstructor
@Api(value = "排班管理相关接口",tags = "排班相关接口")
public class ScheduleController {
    @Autowired
    ScheduleService scheduleService;

    @Autowired
    public SMSUtils smsUtils;

    @Autowired
    public ImageParser parser;

    @GetMapping("/getTodayCounselorScheduleInfo")
    @ApiOperation(value = "查看今天有排班的<咨询师>", notes = "无参")
    public JSONObject getTodayCounselorScheduleInfo(){
        Date date = new Date();
        JSONObject ret = scheduleService.getTodayCounselor(date);
        return ret;
    }

    @GetMapping("/getTodaySupervisorScheduleInfo")
    @ApiOperation(value = "查看今天有排班的<督导>", notes = "无参")
    public JSONObject getTodaySupervisorScheduleInfo(){
        Date date = new Date();
        JSONObject ret = scheduleService.getTodaySupervisor(date);
        return ret;
    }

    @PostMapping("/getCounselorMonthSchedule")
    @ApiOperation(value = "查看指定月份<咨询师>排班计划", notes = "<咨询师>Id+年+月，全部为int类型")
    public JSONObject getCounselorMonthSchedule(@RequestParam("id") int counselorId,@RequestParam("year") int year, @RequestParam("month") int month){
        return scheduleService.getCounselorMonthSchedule(counselorId,year,month);
    }

    @PostMapping("/getSupervisorMonthSchedule")
    @ApiOperation(value = "查看指定月份<督导>排班计划", notes = "<督导>Id+年+月，全部为int类型")
    public JSONObject getSupervisorMonthSchedule(@RequestParam("id") int supervisorId,@RequestParam("year") int year, @RequestParam("month") int month){
        return scheduleService.getSupervisorMonthSchedule(supervisorId,year,month);
    }

    @PostMapping("/getPersonTodaySchedule")
    @ApiOperation(value = "查看当天<咨询师>或<督导>是否有排班", notes = "<咨询师>/<督导>Id+年+月+日，int类型")
    public JSONObject getPersonTodaySchedule(@RequestParam("id") int id, @RequestParam("year") int year,@RequestParam("month") int month,@RequestParam("day")int day){
        Date date = new Date(0);
        date.setYear(year-1900);
        date.setMonth(month-1);
        date.setDate(day);
        return  scheduleService.getPersonTodayStatus(id);
    }

    @PostMapping("/getPersonTodayWeekSchedule")
    @ApiOperation(value = "查看当天<咨询师>或<督导>的周排班计划", notes = "<咨询师>/<督导>Id+年+月+日，int类型")
    public JSONObject getPersonTodayWeekSchedule(@RequestParam("id") int id, @RequestParam("year") int year,@RequestParam("month") int month,@RequestParam("day")int day){
        Date date = new Date(year-1900,month-1,day);
        return  scheduleService.getPersonTodayWeekSchedule(date,id);
    }

    @PostMapping("/adjustMonthSchedule")
    @ApiOperation(value = "更改当月某一天<咨询师>或<督导>的排班计划", notes = "<咨询师>/<督导>Id+年+月+日，int类型,operation为1表示添加，为-1表示删除")
    public JSONObject adjustMonthSchedule(@RequestParam("id") int id, @RequestParam("year") int year,@RequestParam("month") int month,@RequestParam("day")int day,@RequestParam("operation") int operation){
        Date date = new Date(year-1900,month-1,day);
        return  scheduleService.adjustMonthSchedule(date,id,operation);
    }

    @PostMapping("/getTodayBandSchedule")
    @ApiOperation(value = "<督导>查看今天有排班计划并且和自己有绑定关系的<咨询师>", notes = "<咨询师>Id+年+月+日，为int类型")
    public JSONObject getTodayBandSchedule(@RequestParam("id") int id,@RequestParam("year") int year,@RequestParam("month") int month,@RequestParam("day")int day){
        Date date = new Date(year-1900,month-1,day);
        return  scheduleService.getTodayCounselorWithBand(date,id);
    }

    @PostMapping("/getMonthSchedule")
    @ApiOperation(value = "查看当月所有<咨询师>和<督导>的排班", notes = "年+月，为int类型")
    public JSONArray getMonthCounselorSchedule(@RequestParam("year") int year, @RequestParam("month") int month){
        return scheduleService.getMonthSchedule(new Date(year-1900,month-1,1));
    }

    @PostMapping("/getTodayCounselorDetails")
    @ApiOperation(value = "用户视角查看当天值班咨询师", notes = "用户id，String类型")
    public JSONArray getTodayCounselorDetails(@RequestParam("uid") String uid){
        int id = Integer.parseInt(uid);
        return scheduleService.getTodayCounselorDetails(new Date(),id);
    }

    @PostMapping("/getConsultationHistory")
    @Transactional(rollbackFor = Exception.class)
    @ApiOperation(value = "下载历史记录", notes = "download", produces = "application/json")
    public void getHistory(@RequestParam("hid") int hid, HttpServletRequest request, HttpServletResponse response) throws IOException {
        OutputStream outputStream=null;
        InputStream inputStream=null;
        BufferedInputStream bufferedInputStream=null;
        byte[] bytes=new byte[1024];
        File file = new File(scheduleService.exportSingleHistory(hid));
        String fileName = file.getName();
        // 获取输出流
        try {
            response.setHeader("Content-Disposition", "attachment;filename=" + scheduleService.exportSingleHistory(hid));
            response.setContentType("application/force-download");
            inputStream = new FileInputStream(file);
            bufferedInputStream=new BufferedInputStream(inputStream);
            outputStream = response.getOutputStream();
            int i=bufferedInputStream.read(bytes);
            while (i!=-1){
                outputStream.write(bytes,0,i);
                i=bufferedInputStream.read(bytes);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if (inputStream!=null){
                    inputStream.close();
                }
                if (outputStream!=null){
                    outputStream.close();
                }
                if (bufferedInputStream!=null){
                    bufferedInputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }
}